package eaa.tgi.scrum.repositories;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Repository;

import eaa.tgi.scrum.domain.User;

@Repository
public class ProjectRepository {

	protected EntityManager entityManager;
	
	@PersistenceContext
	public void setEntityManager(EntityManager entityManager) {
		this.entityManager = entityManager;
	}
	
	public List<User> getUsers(Long projectId) {
		
		try {
		
			List<User> users = (List<User>) entityManager.createQuery("SELECT u FROM User u WHERE project.id = :project")
										.setParameter("project", projectId).getResultList();
		
			return users;
			
		} catch (NoResultException userNotFoundException) {
			return null;
		}
	}
}
